package org.sky.sys.util;

import org.sky.util.CamelUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;

/**
 * @author weifx
 * @Title: CriteriaUtils
 * @ProjectName springboot-platform
 * @Description: TODO{Example.Criteria工具类}
 * @date 2019/3/7 21:18
 * @Version 1.0
 */
public class CriteriaUtils {


    /**
     * 初始化Example过滤条件
     * @param criteria
     * @param filter
     */
    public static void initFilter(Example.Criteria criteria, Map<String,Object> filter){
        if(null == filter){
            return;
        }
        Iterator<Map.Entry<String, Object>> iterator = filter.entrySet().iterator();
        Map.Entry<String, Object> entry;
        while(iterator.hasNext()){
            entry = iterator.next();
            String key = entry.getKey();
            Object value = entry.getValue();
            String property = key.split("@")[0];//Entity属性
            String operate = key.split("@")[1];//运算符
            if(operate.toUpperCase().equals(Operate.LIKE)){
                criteria.andLike(property,String.valueOf(value));
            }else if(operate.toUpperCase().equals(Operate.IN.toUpperCase())){
                String[] val =
                        String.valueOf(value)
                                .replace("(","")
                                .replace(")","")
                                .split(",");
                criteria.andIn(property, Arrays.asList(val));
            }else if(operate.toUpperCase().replace(" ","")
                        .equals(Operate.NOT_IN.toUpperCase().replace(" ",""))){
                String[] val =
                        String.valueOf(value)
                                .replace("(","")
                                .replace(")","")
                                .split(",");
                criteria.andNotIn(property, Arrays.asList(val));
            }else if(operate.toUpperCase().replace(" ","")
                    .equals(Operate.IS_NULL.toUpperCase().replace(" ",""))){
                criteria.andIsNull(property);
            }else if(operate.toUpperCase().replace(" ","")
                    .equals(Operate.IS_NOT_NULL.toUpperCase().replace(" ",""))){
                criteria.andIsNotNull(property);
            }else if(operate.toUpperCase().equals(Operate.BETWEEN.toUpperCase())){
                String obj1 = String.valueOf(value).split(",")[0];
                String obj2 = String.valueOf(value).split(",")[1];
                criteria.andBetween(property,obj1,obj2);
            }else if(operate.toUpperCase().replace(" ","")
                        .equals(Operate.NOT_BETWEEN.toUpperCase().replace(" ",""))){
                String obj1 = String.valueOf(value).split(",")[0];
                String obj2 = String.valueOf(value).split(",")[1];
                criteria.andNotBetween(property,obj1,obj2);
            }else{
                criteria.andCondition(CamelUtils.camelToUnderline(property)+" "+operate,value);
            }

        }
    }

    /**
     * 数据库操作符
     */
    class Operate{
        public final static String LIKE="LIKE";
        public final static String IN="IN";
        public final static String NOT_IN="NOT IN";
        public final static String IS_NULL="IS NULL";
        public final static String IS_NOT_NULL="IS NOT NULL";
        public final static String BETWEEN="BETWEEN";
        public final static String NOT_BETWEEN="NOT BETWEEN";
    }
}
